/**
 * @filename:SysRoleController 2022年1月27日
 * @project Wonder xhadmin Framework  V1.0
 * Copyright(c) 2020 LuZhongren Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.controller;


import com.wonder.admin.domain.SysRole;
import com.wonder.admin.service.SysRoleService;
import com.wonder.admin.request.SysRoleRequest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**   
 * 
 * <p>说明： 分组权限管理API接口层</P>
 * @version: V1.0
 * @author: LuZhongren
 * @time    2022年1月27日
 *
 */
@Api(tags = "分组权限管理")
@RestController
@RequestMapping("/admin/Role")
public class SysRoleController {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@ApiOperation(value = "分组权限管理列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public AjaxResult<CommonPage<SysRole>> index(@RequestBody SysRoleRequest sysRoleRequest) {
	
		Page<SysRole> page = new Page<SysRole>(sysRoleRequest.getPage(), sysRoleRequest.getLimit());
		IPage<SysRole> list = sysRoleService.selectList(sysRoleRequest, page);
		return AjaxResult.success(CommonPage.restPage(list));
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   对象参数：role_id
	 * @return  JsonResult
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/getInfo")
	@ApiOperation(value = "获取分组权限管理对象", notes = "作者：LuZhongren")
	public AjaxResult<SysRole> getById(@RequestBody Map<String, Object> map){
		if(map==null) return AjaxResult.failed(MessageUtils.message("search.not.exist"));
		Long role_id=Long.parseLong(map.get("role_id").toString());
		SysRole sysRole=sysRoleService.getById(role_id);
		if (null!=sysRole ) {
			 if(sysRole.getAccess_table() !=null)
				 sysRole.setAccess(StringUtils.split(sysRole.getAccess_table(), ','));
			 return AjaxResult.success(sysRole);
		}else {
			return AjaxResult.failed(MessageUtils.message("search.not.exist"));
		}
		
	}
	
	/**
	 * @explain 删除对象
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：LuZhongren")
	public AjaxResult<SysRole> delete(@RequestBody Map<String, Object> map){
		if(map==null) return AjaxResult.failed(MessageUtils.message("search.not.exist"));
		String role_id=map.get("role_id").toString();
		if(StringUtils.isBlank(role_id)) 
			return AjaxResult.failed(MessageUtils.message("input.parameter.error"));
		String[] roles=StringUtils.split(role_id, ',');
		ArrayList<Integer> ids=new ArrayList<Integer>(); 
		for(int i=0;i<roles.length;i++)
		{
		   ids.add( Integer.parseInt(roles[i]));//整数数组
		}
		if(ids.contains(1)) 
			return AjaxResult.failed(MessageUtils.message("superadmin.cannot.delete"));
		boolean rsg = sysRoleService.removeByIds(ids);
		if (rsg) {
			    return AjaxResult.success(MessageUtils.message("delete.success"));
		  }else {
			   return AjaxResult.failed(MessageUtils.message("delete.not.exist"));
		  }
		
	}
	
	/**
	 * @explain 添加
	 * @param   对象参数：SysRole
	 * @return  Boolean
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：LuZhongren")
	public AjaxResult<Long> insert(@RequestBody  SysRole entity){

		if (null!=entity) {
			if(entity.getAccess()!=null)
			{
				List<String> access=Arrays.asList(entity.getAccess());
				if(!access.contains("Home")) 
					access.add(0, "Home");
				entity.setAccess_table(StringUtils.join(access.toArray(), ","));
				    
			}
			boolean rsg = sysRoleService.save(entity);
			if (rsg) {
				  return AjaxResult.success(entity.getRole_id(),MessageUtils.message("insert.success"));
			  }else {
				  return AjaxResult.failed(MessageUtils.message("insert.fail"));
			  }
		}else {
			return AjaxResult.failed(MessageUtils.message("insert.parameter.error"));
		}
	}
	
	/**
	 * @explain 修改
	 * @param   对象参数：SysRole
	 * @return  Boolean
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：LuZhongren")
	public AjaxResult<SysRole> update(@RequestBody SysRole entity){
		if (null!=entity) {
			if(entity.getAccess()!=null)
			{
				List<String> access=Arrays.asList(entity.getAccess());
				if(!access.contains("Home")) 
					access.add(0, "Home");
				entity.setAccess_table(StringUtils.join(access.toArray(), ","));
				    
			}
			boolean rsg = sysRoleService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success(MessageUtils.message("update.success"));
			  }else {
				  return AjaxResult.failed(MessageUtils.message("update.fail"));
			  }
		}else {
			return AjaxResult.failed(MessageUtils.message("update.not.exist"));
		}
	}
	
	
	/**
	 * @explain 修改
	 * @param   对象参数：SysRole
	 * @return  Boolean
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改状态排序", notes = "作者：LuZhongren")
	public AjaxResult<SysRole> updateEx(@RequestBody SysRole entity){
		if (null!=entity) {	
			boolean rsg = sysRoleService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success(MessageUtils.message("update.success"));
			  }else {
				  return AjaxResult.failed(MessageUtils.message("update.fail"));
			  }
		}else {
			return AjaxResult.failed(MessageUtils.message("update.not.exist"));
		}
	}
	
	
	/**
	 * @explain 获取权限菜单列表
	 * @param   对象参数：SysRole
	 * @return  Boolean
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/getRoleMenus")
	@ApiOperation(value = "获取权限菜单列表", notes = "作者：LuZhongren")
	public AjaxResult<SysRole> getRoleMenus(@RequestBody SysRole entity){
		if (null!=entity) {	
			boolean rsg = sysRoleService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success(MessageUtils.message("update.success"));
			  }else {
				  return AjaxResult.failed(MessageUtils.message("update.fail"));
			  }
		}else {
			return AjaxResult.failed(MessageUtils.message("update.not.exist"));
		}
	}
	
	
}